home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / earthlink / nscomm / java40.jar / java / lang / UNIXProcess.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-03  |  3.5 KB  |  151 lines

  1. package java.lang;
  2.  
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileDescriptor;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.PipedOutputStream;
  11. import java.util.Hashtable;
  12.  
  13. class UNIXProcess extends Process implements Runnable {
  14.    static Hashtable subprocs = null;
  15.    private boolean isalive = false;
  16.    private int exit_code;
  17.    private FileDescriptor stdin_fd;
  18.    private FileDescriptor stdout_fd;
  19.    private FileDescriptor stderr_fd;
  20.    private FileDescriptor sync_fd;
  21.    int pid;
  22.    private OutputStream stdin_stream;
  23.    private InputStream raw_stdout;
  24.    private InputStream raw_stderr;
  25.    private ProcessInputStream piped_stdout_in;
  26.    private ProcessInputStream piped_stderr_in;
  27.    private PipedOutputStream piped_stdout_out;
  28.    private PipedOutputStream piped_stderr_out;
  29.    private int numReaders;
  30.  
  31.    private UNIXProcess() {
  32.    }
  33.  
  34.    public native void run();
  35.  
  36.    private native int forkAndExec(String[] var1, String[] var2) throws IOException;
  37.  
  38.    private native void notifyReaders();
  39.  
  40.    private static void deadChild(int var0, int var1) {
  41.       UNIXProcess var2 = (UNIXProcess)subprocs.get(new Integer(var0));
  42.       if (var2 != null) {
  43.          synchronized(var2){}
  44.  
  45.          try {
  46.             var2.isalive = false;
  47.             subprocs.remove(new Integer(var0));
  48.             var2.exit_code = var1;
  49.             var2.notifyReaders();
  50.             var2.notifyAll();
  51.          } catch (Throwable var5) {
  52.             throw var5;
  53.          }
  54.  
  55.       }
  56.    }
  57.  
  58.    synchronized int getNumReaders() throws InterruptedException {
  59.       return this.numReaders;
  60.    }
  61.  
  62.    synchronized void decrNumReaders() {
  63.       if (--this.numReaders <= 0) {
  64.          try {
  65.             this.stdin_stream.close();
  66.          } catch (IOException var3) {
  67.          }
  68.  
  69.          try {
  70.             this.raw_stdout.close();
  71.          } catch (IOException var2) {
  72.          }
  73.  
  74.          try {
  75.             this.raw_stderr.close();
  76.          } catch (IOException var1) {
  77.          }
  78.       }
  79.  
  80.       this.notifyAll();
  81.    }
  82.  
  83.    UNIXProcess(String[] var1, String[] var2) throws IOException {
  84.       this.stdin_fd = new FileDescriptor();
  85.       this.stdout_fd = new FileDescriptor();
  86.       this.stderr_fd = new FileDescriptor();
  87.       this.sync_fd = new FileDescriptor();
  88.       this.pid = this.forkAndExec(var1, var2);
  89.       this.isalive = true;
  90.       SecurityManager.enablePrivilege("UniversalFdRead");
  91.       SecurityManager.enablePrivilege("UniversalFdWrite");
  92.       SecurityManager.enablePrivilege("UniversalThreadAccess");
  93.       this.stdin_stream = new BufferedOutputStream(new FileOutputStream(this.stdin_fd));
  94.       this.raw_stdout = new FileInputStream(this.stdout_fd);
  95.       this.raw_stderr = new FileInputStream(this.stderr_fd);
  96.       this.piped_stdout_out = new PipedOutputStream();
  97.       this.piped_stderr_out = new PipedOutputStream();
  98.       this.piped_stdout_in = new ProcessInputStream(this, this.piped_stdout_out, this.raw_stdout);
  99.       this.piped_stderr_in = new ProcessInputStream(this, this.piped_stderr_out, this.raw_stderr);
  100.       Thread var3 = new Thread(this.piped_stdout_in, "stdout reader pid=" + this.pid);
  101.       Thread var4 = new Thread(this.piped_stderr_in, "stderr reader pid=" + this.pid);
  102.       this.numReaders = 2;
  103.       var3.start();
  104.       var4.start();
  105.       subprocs.put(new Integer(this.pid), this);
  106.       FileOutputStream var5 = new FileOutputStream(this.sync_fd);
  107.       var5.write(65);
  108.       var5.close();
  109.       SecurityManager.revertPrivilege();
  110.    }
  111.  
  112.    public OutputStream getOutputStream() {
  113.       return this.stdin_stream;
  114.    }
  115.  
  116.    public InputStream getInputStream() {
  117.       return this.piped_stdout_in;
  118.    }
  119.  
  120.    public InputStream getErrorStream() {
  121.       return this.piped_stderr_in;
  122.    }
  123.  
  124.    public synchronized int waitFor() throws InterruptedException {
  125.       while(this.isalive) {
  126.          this.wait();
  127.       }
  128.  
  129.       return this.exit_code;
  130.    }
  131.  
  132.    public synchronized int exitValue() {
  133.       if (this.isalive) {
  134.          throw new IllegalThreadStateException("process hasn't exited");
  135.       } else {
  136.          return this.exit_code;
  137.       }
  138.    }
  139.  
  140.    public native void destroy();
  141.  
  142.    static {
  143.       SecurityManager.enablePrivilege("UniversalThreadAccess");
  144.       subprocs = new Hashtable();
  145.       Thread var0 = new Thread(new UNIXProcess(), "process reaper");
  146.       var0.setDaemon(true);
  147.       var0.start();
  148.       SecurityManager.revertPrivilege();
  149.    }
  150. }
  151.